package Teste;

import java.awt.Color;

import javax.swing.*;
 
import org.math.plot.*;
import org.math.plot.utils.Array;
 
import static java.lang.Math.*;
 
public class Teste {
        public static void main(String[] args) {
 
                // define your data
        		double r = 1;
        		int n = 26; // Circulo em 20 partes
        		
                double[] x = new double[1 + n/2]; //increment(1.0, -0.04, 0.0); // x = 0.0:0.1:1.0
                double[] y = new double[1 + n/2]; //increment(0.0, 0.04, 1.0); // y = 0.0:0.05:1.0
                double theta = (2 * Math.PI)/n;

        		for (int i = 0; i < x.length; i++){
        			x[i] = r * Math.cos(i * theta);
        		}
                
                double[][] z1 = f1(x, y);
                double[][] z2 = f2(x, y);
                double[][] z3 = f3(x, y);
                //double[] z4 = f4(x, y);
 
                // create your PlotPanel (you can use it as a JPanel) with a legend at SOUTH
                Plot3DPanel plot = new Plot3DPanel("SOUTH");
                plot.removeLegend();
                plot.removePlotToolBar();
                
                
                // add grid plot to the PlotPanel
                //int numPlot = plot.addGridPlot("z=cos(PI*x)*sin(PI*y)", x, y, z1);
                
                double[][] xyz = new double[n+1][3];
                
                double z = 0;
                int[] k = new int[1 + n/2];
                for (int j = 0; j < 1 + n/2; j++){
                	r = Math.sin(j * Math.PI / (n/2 - 1));
                	z = Math.cos(j * Math.PI / (n/2 - 1));
	                for (int i = 0; i < n+1; i++){
	                	// Pontos da circunferencia paralelas ao plano XY
	                	xyz[i][0] = r * Math.cos(i * theta);
	                	xyz[i][1] = r * Math.sin(i * theta);
	                	xyz[i][2] = z;
	                	
	                }
	                k[j] = plot.addLinePlot("circle", Color.blue, Array.copy(xyz));
                }

                double[][] xyz2 = new double[k.length][3]; // Pontos longitudinais
                for (int i = 0; i < n + 1; i++){
                    for (int j = 0; j < k.length; j++){
                    	// Pontos da circunferencia perpendiculares ao plano XY
                    	xyz2[j] = plot.getPlot(k[j]).getData()[i];
                    }
                    plot.addLinePlot("esfera", Color.blue, Array.copy(xyz2));
                    
                }

                
//                plot.getAxis(0).setGridVisible(false);
//                plot.getAxis(1).setGridVisible(false);
//                plot.getAxis(2).setGridVisible(false);
                
                //((GridPlot3D)plot.getPlot(numPlot)).draw_lines = false;
                //((GridPlot3D)plot.getPlot(numPlot)).draw_dots = false;
                //((GridPlot3D)plot.getPlot(numPlot)).color = Color.red;
                //plot.addGridPlot("z=sin(PI*x)*cos(PI*y)", x, y, z2);

                //plot.addLinePlot(x, y, z4);
                
                // put the PlotPanel in a JFrame like a JPanel
                JFrame frame = new JFrame("a plot panel");
                frame.setSize(600, 600);
                frame.setContentPane(plot);
                frame.setVisible(true);
 
        }
 
        // function definition: z=cos(PI*x)*sin(PI*y)
        public static double f1(double x, double y) {
                double z = x - y; //cos(x * PI) * sin(y * PI);
                return z;
        }
 
        // grid version of the function
        public static double[][] f1(double[] x, double[] y) {
                double[][] z = new double[y.length][x.length];
                for (int i = 0; i < x.length; i++)
                        for (int j = 0; j < y.length; j++)
                                z[j][i] = f1(x[i], y[j]);
                return z;
        }
 
        // another function definition: z=sin(PI*x)*cos(PI*y)
        public static double f2(double x, double y) {
                double z = sin(x * PI) * cos(y * PI);
                return z;
        }
 
        // grid version of the function
        public static double[][] f2(double[] x, double[] y) {
                double[][] z = new double[y.length][x.length];
                for (int i = 0; i < x.length; i++)
                        for (int j = 0; j < y.length; j++)
                                z[j][i] = f2(x[i], y[j]);
                return z;
        }
        
        public static double[][] f3(double[] x, double[] y) {
            double[][] z = new double[y.length][x.length];
            for (int i = 0; i < x.length; i++){
                for (int j = 0; j < y.length; j++){
                	z[j][i] = 1 - Math.pow(x[i], 2) - Math.pow(y[j], 2);
                	if (z[j][i] < 0)
                		z[j][i] = 0;
                	else
                		z[j][i] = Math.sqrt(z[j][i]);
                }
            }
            return z;
        }
        
        public static double[][] f4(double[] x, double[] y) {
            double[][] z = new double[y.length][x.length];
            for (int i = 0; i < x.length; i++)
                    for (int j = 0; j < y.length; j++)
                            z[j][i] = f2(x[i], y[j]);
            return z;
    }
}